package net.suncaper.demo.service;

import net.suncaper.demo.mapper.UserMapper;
import net.suncaper.demo.domain.User;
import net.suncaper.demo.domain.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public List<User> findUser(){
        return userMapper.selectByExample(new UserExample());
    }

    @Override
    public void saveUser(User user) {
        userMapper.insert(user);
    }

    @Override
    public void deleteUserById(long userid) {
        UserExample userExample=new UserExample();
        userExample.createCriteria().andUseridEqualTo(userid);
        userMapper.deleteByExample(userExample);
    }

    @Override
    public User findUserById(long userid) {
        UserExample userExample=new UserExample();
        userExample.createCriteria().andUseridEqualTo(userid);
        return userMapper.selectByExample(userExample).get(0);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public List<User> findUserByTeleAndName(String username, String telephone) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria1= userExample.createCriteria();
        userExample.setDistinct(true);
        if(username!="" && username!=null){
            criteria1.andUsernameLike("%"+username+"%");
        }
        if(telephone!="" && telephone!=null){
            criteria1.andTelephoneLike("%"+telephone+"%");
        }

        List<User> users = userMapper.selectByExample(userExample);

        return users;
    }
}


